package com.dragon.www.smarthome.fragment;

import android.os.Bundle;
import android.support.percent.PercentRelativeLayout;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.dragon.www.smarthome.R;
import com.dragon.www.smarthome.entity.Device;
import com.dragon.www.smarthome.entity.Room;
import com.dragon.www.smarthome.popupwindow.AirconditionPopWindow;
import com.dragon.www.smarthome.popupwindow.BasePopupWindow;
import com.dragon.www.smarthome.popupwindow.ConferencePopWindow;
import com.dragon.www.smarthome.popupwindow.ConferenceSetPopWindow;
import com.dragon.www.smarthome.view.MainItemView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by daiye on 2016/6/1.
 */
public class SecondScreenFragment extends Fragment implements View.OnClickListener {
    private PercentRelativeLayout secondscreenMainView;
    private View mLineView;

    private MainItemView mainViewAircondition;

    private int popWindowHeight;
    private int popWindowWidth;
    private final static float xToScreenPercent = 0.9277f;
    private final static float yToScreenPercent = 0.6551f;
    private int xOffsetToView;
    private int yOffsetToView;
    private final static float xToView = 0.0361f;
    private final static float yToView = 0.0f;
    private int conferencePopWindowHeight;


    private BasePopupWindow myPopWindow;

    private int width;
    private int height;

    private List<Room> mRooms = null;
    private List<Device> mDevices = null;
    private Room mSelectRoom = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View entryView = inflater.inflate(R.layout.fragment_secondscreen, null);
        initView(entryView);
        return entryView;
    }

    private void initView(View entryView){
        secondscreenMainView = (PercentRelativeLayout) entryView.findViewById(R.id.secondscreen_mainView);
        mLineView = entryView.findViewById(R.id.line);
        mainViewAircondition = (MainItemView) entryView.findViewById(R.id.mainView_aircondition);
        mainViewAircondition.setOnClickListener(this);
    }

    private void initData(){
        WindowManager wm = getActivity().getWindowManager();
        width = wm.getDefaultDisplay().getWidth();
        height = wm.getDefaultDisplay().getHeight();
        popWindowHeight = (int) (height * yToScreenPercent);
        popWindowWidth = (int) (width * xToScreenPercent);
        xOffsetToView = (int) (width * xToView);
        yOffsetToView = (int) (height * yToView);
        conferencePopWindowHeight = (int) (height * 0.781);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.mainView_aircondition:
                myPopWindow = new AirconditionPopWindow(getContext());
                break;
            default:
                break;
        }

        if (myPopWindow != null && mSelectRoom != null) {
            myPopWindow.setRoomId(mSelectRoom.getRoomid());
            myPopWindow.setDevices(mDevices);
            myPopWindow.setWidth(popWindowWidth);
            myPopWindow.setHeight(conferencePopWindowHeight);
            myPopWindow.showPopWindow(mLineView, xOffsetToView, (int) (yOffsetToView  + 0.5f));
            myPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    //弹出窗消失时显示主布局
                    showMainView();
                }
            });
            //隐藏主布局
            hideMainView();
        }else if(myPopWindow != null){
            Toast.makeText(getContext(), "您还没有登录或选择任何房间！", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 从网络获取房间后设置对应的app设备，包括自动恢复上次保存的room信息
     * 2016/06/01
     */
    public void setRooms(List<Room> rooms){
        this.mRooms = rooms;
    }

    public void setDevices(List<Device> devices) {
        this.mDevices = devices;
    }

    /**
     * 在房间界面选中某一个房间后，切换到该房间，包括保存到本地
     * 2016/06/01
     */
    public void selectRoom(Room room){
        this.mSelectRoom = room;
    }

    /**
     * 显示设备界面的时候隐藏主方块界面
     * 2016/06/01
     */
    public void hideMainView() {
        secondscreenMainView.setVisibility(View.GONE);
        if(mListener!=null){
            mListener.onHideOperateBar();
        }
    }

    /**
     * 设备对话框关闭的时候显示主方块界面
     * 2016/06/01
     */
    public void showMainView() {
        secondscreenMainView.setVisibility(View.VISIBLE);
        if(mListener!=null){
            mListener.onShowOperateBar();
        }
    }

    /**
     * 将该FirstScreenFragment的一些操作映射到它的父容器，操作父容器的界面
     * 2016/06/01
     */
    private OnOperateListener mListener = null;

    public void setOnOperateListener(OnOperateListener l) {
        mListener = l;
    }

    public interface OnOperateListener {
        void onHideOperateBar();
        void onShowOperateBar();
    }
}
